//
//  CityTeamViewController.m
//  LanQiuQuan
//
//  Created by TonyGenius on 16/4/12.
//  Copyright © 2016年 Rip. All rights reserved.
//

#import "CityTeamViewController.h"
#import "Marco.h"
#import "SHSearchView.h"
@interface CityTeamViewController ()<UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
{
    NSMutableArray *_cityTeamList;
    XMTabBarController *_tab;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation CityTeamViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.automaticallyAdjustsScrollViewInsets = YES;
    _cityTeamList = [NSMutableArray array];
    [self initTabelView];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    backItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backItem;
    self.navigationItem.title = @"同城球队";
}

- (void)viewWillAppear:(BOOL)animated
{
    _tab = (XMTabBarController *)self.tabBarController;
    [_tab xmTabBarHidden:YES animated:YES];
}
- (void)initTabelView
{
    [_tableView registerNib:[UINib nibWithNibName:@"myTeamsTableViewCell" bundle:nil] forCellReuseIdentifier:kMYTEAMSCELL];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableViewBG"]];
    [_tableView setBackgroundView:imageView];
    __weak CityTeamViewController *weakSelf = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requestToTop];
    }];
    
//    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf requestToBottom];
//    }];
    [_tableView.mj_header beginRefreshing];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initArray
{
    _cityTeamList = [NSMutableArray array];
}

- (void)requestToTop
{
    
    [NetWorkEngin getRequestWithParametes:kURLFORCITYTEAM andBody:nil andFinishBlock:^(NSDictionary *respondData, NSError *error) {
        [_cityTeamList removeAllObjects];
        
        [_cityTeamList addObjectsFromArray:[jsonHelper jsonForCityTeamWithDictionary:respondData]];
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
        
    }];
}

- (void)requestToBottom
{
//    NSString *urlForPlayGround = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [NetWorkEngin getRequestWithParametes:kURLFORCITYTEAM andBody:nil andFinishBlock:^(NSDictionary *respondData, NSError *error) {
        
        [_cityTeamList addObjectsFromArray:[jsonHelper jsonForCityTeamWithDictionary:respondData]];
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    }];
    
}


#pragma mark -UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    myTeamsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMYTEAMSCELL forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TeamModel *model = _cityTeamList[indexPath.row];
//    cell.teamName.text = model.name;
    [cell.nameBtn setTitle:model.name forState:UIControlStateNormal];
    cell.winTimes.text = [NSString stringWithFormat:@"%@胜%@负",model.winTimes,model.lostTimes];
    cell.teamPlayers.text = [NSString stringWithFormat:@"%@/100",model.peopleCount];
    NSLog(@"%@___%@",model.winTimes,model.peopleCount);
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    SHSearchView *search = [[NSBundle mainBundle] loadNibNamed:@"SHSearchView" owner:self options:nil][0];
    search.searchBar.placeholder = @"查找球队";
    
    search.frame = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
    search.backgroundColor = [UIColor clearColor];
    [view addSubview:search];
    
    return view;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cityTeamList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma  mark -UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 93;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    myTeamsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundImage.image = [UIImage imageNamed:@"tableCellS"];
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    myTeamsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundImage.image = [UIImage imageNamed:@"tableCellBG"];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
